package com.lewis.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lewis.core.annotation.Log;
import com.lewis.core.base.controller.BaseController;
import com.lewis.core.base.domain.BaseResult;
import com.lewis.core.base.domain.entity.SysDictType;
import com.lewis.core.base.page.TableDataInfo;
import com.lewis.core.constant.UserConstants;
import com.lewis.core.enums.BusinessType;
import com.lewis.core.utils.SecurityUtil;
import com.lewis.core.utils.office.excel.ExcelUtil;
import com.lewis.system.service.ISysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 数据字典信息
 *
 * @author Lewis
 */
@Api(tags = "数据字典信息")
@RestController
@RequestMapping("/system/dict/type")
public class SysDictTypeController extends BaseController {

	@Resource
	private ISysDictTypeService dictTypeService;

	@ApiOperation("字典类型-列表")
	@GetMapping("/list")
	public TableDataInfo list(SysDictType dictType) {
		startPage();
		return getTableData(dictTypeService.selectDictTypeList(dictType));
	}

	@ApiOperation("字典类型-导出")
	@Log(
			title = "字典类型",
			businessType = BusinessType.EXPORT
	)
	@GetMapping("/export")
	public BaseResult export(SysDictType dictType) {
		List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
		ExcelUtil<SysDictType> util = new ExcelUtil<SysDictType>(SysDictType.class);
		return util.exportExcel(list, "字典类型");
	}

	@ApiOperation("字典类型-详情")
	@GetMapping("/{dictId}")
	public BaseResult getInfo(@PathVariable Long dictId) {
		return ok().setData(dictTypeService.getOne(
				new LambdaQueryWrapper<SysDictType>()
						.eq(SysDictType::getDictId, dictId)
		));
	}

	@ApiOperation("字典类型-新增")
	@Log(
			title = "字典类型",
			businessType = BusinessType.INSERT
	)
	@PostMapping
	public BaseResult add(@Validated @RequestBody SysDictType dict) {
		if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict))) {
			return fail().setMsg("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
		}
		dict.setCreateBy(SecurityUtil.getUsername());
		return getResult(dictTypeService.save(dict));
	}

	@ApiOperation("字典类型-修改")
	@Log(
			title = "字典类型",
			businessType = BusinessType.UPDATE
	)
	@PutMapping
	public BaseResult edit(@Validated @RequestBody SysDictType dict) {
		if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict))) {
			return fail().setMsg("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
		}
		dict.setUpdateBy(SecurityUtil.getUsername());
		return getResult(dictTypeService.updateDictType(dict));
	}

	@ApiOperation("字典类型-删除[支持批量删除]")
	@Log(
			title = "字典类型",
			businessType = BusinessType.DELETE
	)
	@DeleteMapping("/{dictIds}")
	public BaseResult remove(@PathVariable Long[] dictIds) {
		dictTypeService.deleteDictTypeByIds(dictIds);
		return ok();
	}

	@ApiOperation("字典-缓存刷新")
	@Log(
			title = "字典类型",
			businessType = BusinessType.CLEAN
	)
	@DeleteMapping("/refreshCache")
	public BaseResult refreshCache() {
		dictTypeService.resetDictCache();
		return ok();
	}

	@ApiOperation("字典-选择框列表")
	@GetMapping("/optionselect")
	public BaseResult optionSelect() {
		return ok().setData(dictTypeService.list());
	}
}
